package com.css.fxfzcpxx.modules.infoRetrieval.emergencyShelter.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.css.fxfzcpxx.modules.infoRetrieval.emergencyShelter.entity.JcEmergcyShelter;
import com.css.fxfzcpxx.modules.infoRetrieval.emergencyShelter.entity.SysDataVersion;
import com.css.fxfzcpxx.modules.infoRetrieval.emergencyShelter.param.JcEmergcyShelterParam;
import com.css.fxfzcpxx.modules.infoRetrieval.emergencyShelter.service.EmergencyShelterService;


import com.css.fxfzcpxx.modules.thematicMap.entity.AchManageEntity;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @Author:
 * @CreateTime: 2023/8/10 20:19
 * @Description:
 */
@Service
public class EmergencyShelterServiceImpl implements EmergencyShelterService {

    @Resource(name = "httpsRestTemplates")
    RestTemplate restTemplate;

    @Override
    public SysDataVersion getLatestVersion() {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/datacenter/jcEmergcyShelter/getLatestVersion";
        JSONObject valueMap = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzcpxx");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);

        JSONObject data = restTemplate.exchange(url, HttpMethod.GET, r, JSONObject.class).getBody().getJSONObject("data");

        SysDataVersion sysDataVersion = JSON.parseObject(data.toJSONString(), SysDataVersion.class);

        return sysDataVersion;
    }

    @Override
    public Map<String, Object> getPage(JcEmergcyShelterParam param, int curPage, int pageSize) {
        StringBuilder queryStr=new StringBuilder();
        if(PlatformObjectUtils.isNotEmpty(param.getProvince())){
            queryStr.append("&province="+param.getProvince()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getCity())){
            queryStr.append("&city="+param.getCity()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getCounty())){
            queryStr.append("&county="+param.getCounty()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getShelterClass())){
            queryStr.append("&shelterClass="+param.getShelterClass()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getAreaMax())){
            queryStr.append("&areaMax="+param.getAreaMax()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getAreaMin())){
            queryStr.append("&areaMin="+param.getAreaMin()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getPoprefMax())){
            queryStr.append("&poprefMax="+param.getPoprefMax()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getPoprefMin())){
            queryStr.append("&poprefMin="+param.getPoprefMin()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getGeom())){
            queryStr.append("&geom="+param.getGeom()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getBncsmc())){
            queryStr.append("&bncsmc="+param.getBncsmc()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getDataVersion())){
            queryStr.append("&dataVersion="+param.getDataVersion()+"");
        }

        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/datacenter/jcEmergcyShelter/dzzhfxfzcpxxfwzxt/page?page="+curPage+"&rows="+pageSize+"";
        JSONObject valueMap = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzcpxx");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        String url1 = new StringBuilder(url).append(queryStr).toString();
        JSONObject data = restTemplate.exchange(url1, HttpMethod.GET, r, JSONObject.class).getBody().getJSONObject("data");
        String rows = data.getString("list");
        List<Map> list = JSON.parseObject(rows, new TypeReference<List<Map>>() {});
        Integer total = Integer.valueOf(data.getString("total"));
        Map<String, Object> page = new HashMap<>();
        page.put("total", total);
        page.put("rows", list);
        return page;
    }
    /*@Override
    public Map<String, Object> getPage(JcEmergcyShelterParam param) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/datacenter/jcEmergcyShelter/dzzhfxfzcpxxfwzxt/page";
        JSONObject valueMap = new JSONObject();
        valueMap.put("province", param.getProvince());
        valueMap.put("city", param.getCity());
        valueMap.put("county", param.getCounty());
        valueMap.put("shelterClass", param.getShelterClass());

        valueMap.put("areaMax", param.getAreaMax());

        valueMap.put("areaMin", param.getAreaMin());

        valueMap.put("poprefMax", param.getPoprefMax());


        valueMap.put("poprefMin", param.getPoprefMin());

        valueMap.put("geom", param.getGeom());
        valueMap.put("bncsmc", param.getBncsmc());
        valueMap.put("dataVersion", param.getDataVersion());
        valueMap.put("rows", param.getRows());
        valueMap.put("page", param.getPage());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzcpxx");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        JSONObject data = restTemplate.exchange(url, HttpMethod.POST, r, JSONObject.class).getBody().getJSONObject("data");
        String rows = data.getString("list");
        List<Map> list = JSON.parseObject(rows, new TypeReference<List<Map>>() {
        });
        Integer total = Integer.valueOf(data.getString("total"));
        Map<String, Object> page = new HashMap<>();
        page.put("total", total);
        page.put("rows", list);
        return page;
    }*/

    @Override
    public JcEmergcyShelter findById(String id) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/datacenter/jcEmergcyShelter/dzzhfxfzcpxxfwzxt/findById?id="+id+"";
        JSONObject valueMap = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzcpxx");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);

        JSONObject data = restTemplate.exchange(url, HttpMethod.GET, r, JSONObject.class).getBody().getJSONObject("data");

        JcEmergcyShelter jcEmergcyShelter = JSON.parseObject(data.toJSONString(), JcEmergcyShelter.class);

        return jcEmergcyShelter;
    }
}
